/* Copyright Andy Mitchell 2007. No warranties */

function Lightbox(gtdinboxPage, elParent, type){
	this._gtdinboxPage = gtdinboxPage;
	this._doc = gtdinboxPage.doc;
	this._labels = gtdinboxPage._labels;
	this._eventObservers = new EventObservers(this);
	this._eventHandlers = new EventHandlers_Mutator(this);
	this._xmlHttpRequest = new GIXMLHttpRequest(gtdinboxPage);
	
	this._create(elParent);
	this.setType(type);
};


Lightbox.prototype._create = function(elParent){
	this.el = this._doc.createElement("DIV");
		this.el.className = "lightbox";
		this.el.setAttribute("style", "position:fixed;z-index:5000;top:0px;left:0px;display:none");
	if( elParent ) elParent.appendChild( this.el );
	
	this.elContainer = this._doc.createElement("DIV");
		this.elContainer.className = "lightbox-container";
		this.elContainer.setAttribute("style", "position:relative;z-index:5001;");
	this.el.appendChild( this.elContainer );
};

Lightbox.prototype.setType = function(type, options){
try{
	this._type = type;
		
	var el = this._doc.createElement("DIV");
	if( type=="welcome" ){
		var msg = GTDFileIO.readFromUrl("chrome://gtdinbox/locale/welcome.htm");
		var match = msg.match(/<body[^>]*>((.|\r|\n|\t|\e|\f|\a|\v)+)<\/body>/i);
		el.innerHTML = match[1];
		this.content(el);
		
		this._createBasicLabels();
		
		var elProceed = this._doc.getElementById("gtd-welcome-proceed");
		this._eventObservers.observe(elProceed, "mousedown", this._welcomeProceed, false);
		
		this._createBasicLabels();
	}else if( type=="update" ){
		var msg = GTDFileIO.readFromUrl("chrome://gtdinbox/locale/update.htm");
		var match = msg.match(/<body[^>]*>((.|\r|\n|\t|\e|\f|\a|\v)+)<\/body>/i);
		el.innerHTML = match[1];
		var apply = GIPROTOTYPE.bind(this, function(){
			this.content(el);
			var elMigrate = this._doc.getElementById("gtd-update-migrate");
			this._eventObservers.observe(elMigrate, "mousedown", this._updateMigrate, false);
			var elProceed = this._doc.getElementById("gtd-update-proceed");
			this._eventObservers.observe(elProceed, "mousedown", this._updateProceed, false);
			
			this._retainOldPrefixes();	// keep any old labels if labels found, otherwise use latest
			this._createBasicLabels();	// for any that don't exist - if deleted since last installed
		});
		setTimeout(apply, 200);
	}else if( type=="options" ){
		var msg = GTDFileIO.readFromUrl("chrome://gtdinbox/locale/options.htm");
		var match = msg.match(/<body[^>]*>((.|\r|\n|\t|\e|\f|\a|\v)+)<\/body>/i);
		el.innerHTML = match[1];
		this.content(el);
				
		this._options = new Options(this._gtdinboxPage, el, this, options);
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("Lightbox setType\n" + e.toString());
}
};

Lightbox.prototype._welcomeProceed = function(event){
	// Just show Gmail
	this.active(false);
};
Lightbox.prototype._updateMigrate = function(event){
	// Load the options, with defaults included
	// Update the current labels to be 1.33 (or if not relevant, latest); and restore any missing defaults. Then move to migrate to latest.
	
	var settings = this._gtdinboxPage.gtdinboxBrowser.createDefaultSettings();
	var newTypes = settings.labels.types;		
	this.setType("options", {prefixNewTypes:newTypes});
};
Lightbox.prototype._updateProceed = function(event){
	this.active(false);
};


Lightbox.prototype._retainOldPrefixes = function(){
	// For each prefix, if any instance (label) with old prefix; keep that old prefix. If not, use new prefix.
	
	var labels = this._labels.getLabelsArray();
	var settings = this._gtdinboxPage.gtdinboxBrowser.settings;
	var options = this._gtdinboxPage.gtdinboxBrowser.options;
	for( l in settings.labels.types ){
		var oldKey = l.replace(/s$/, "");
		if( /se$/.test(oldKey) ) oldKey = oldKey.replace(/e$/, "");
		var oldPrefix = options.getOption("char", "GTDInbox_Prefix_" + oldKey );
		var found = false;
		for( var i = 0; i < labels.length; i++ ){
			if( labels[i].indexOf(oldPrefix)==0 ){	// Old Prefix matches, retain it
				found = true;
				break;
			}
		}
		if( found ){
			this._gtdinboxPage.gtdinboxBrowser.settings.labels.types[l].prefix = oldPrefix;
		}else{
		}
	}
	this._gtdinboxPage.gtdinboxBrowser.saveSettings();
};
Lightbox.prototype._createBasicLabels = function(callback){
try{
	// Using the current prefix (set correctly elsewhere); see if any labels are missing. If so, add them.
		
	var at = this._gtdinboxPage.gmailDOM.local("at");
	if( !callback && !at ){
		setTimeout( GIPROTOTYPE.bind(this, function(){this._createBasicLabels(true)}), 2000);
	}
		
	var labelsHash = this._labels.getLabelsHash();
	
	var settings = this._gtdinboxPage.gtdinboxBrowser.settings;
	//settings.labels.types["Statuses"]
	
	var labels = [];
	for( t in settings.labels.types ){
		if( settings.labels.types[t].defaults ){
			var prefix = settings.labels.types[t].prefix;
			for( d in settings.labels.types[t].defaults ){
				var label = prefix + settings.labels.types[t].defaults[d];
				if( !labelsHash[label] ) labels.push(label);
			}
		}
	}
	
	
	
	var finalCallback = GIPROTOTYPE.bind(this, function(){
				var el = this._gtdinboxPage.gmailDOM.element("inbox-link");
				GIPROTOTYPE.doMouseEvent(this._doc, el, "click");	// refresh
			});
	if( labels.length>0 ) var at = this._gtdinboxPage.gmailDOM.addLabel(labels, null, finalCallback);
	
	/*
	var count = {i:0};
	// Add all of labels now
	for( var i = 0; i < labels.length; i++ ){
		var request = {};
		request.url = "http://mail.google.com/mail/?ui=2";
		request.url += "&ik=" + ik
		request.url += "&at=" + at;
		request.url += "&view=up&act=cc_" + encodeURI(labels[i]) + "&rt=h";
		request.callback = GIPROTOTYPE.bind(this, function(){
				if( --count.i==0 ){
					var el = this._gtdinboxPage.gmailDOM.element("inbox-link");
					GIPROTOTYPE.doMouseEvent(this._doc, el, "click");	// refresh
				}
			});
		count.i++;
		this._xmlHttpRequest.process(request);
	}
	*/
}catch(e){
	if( GTDDEBUGMODE ) alert("lightbox createbasiclabels\n" + e.toString());
}
};



Lightbox.prototype.active = function(v){
	if( typeof v=="undefined" ) return this.el.style.display!="none";
	if( v ){
		this._eventHandlers.broadcast("beforeactivate");
		this.el.style.display = "";
		this._eventObservers.observe(window, "resize", this._position, false );
		this._eventHandlers.broadcast("activated");
		this._position();
		
	}else{
		this.el.style.display = "none";
		this._eventObservers.stopObserving(window, "resize", this._position, false );
		this._eventHandlers.broadcast("deactivate");
		this.lastDims = null;
	}
		
};

Lightbox.prototype.content = function(el){
try{
	if( typeof el=="undefined" ) return this.elContent;
	
	while( this.elContainer.firstChild ) this.elContainer.removeChild( this.elContainer.firstChild );
	
	
	var el2 = el.cloneNode(el, true);
	el2.style.display = "";
	el2.style._position = "absolute";
	el2.style.top = "-10000px";
	this._doc.body.appendChild( el2 );
	var dims = GIPROTOTYPE.getDimensions(el2);
	this._doc.body.removeChild( el2 );

	this.elContainer.style.width = dims.width + "px";
	this.elContainer.style.height = dims.height + "px";
		
	this._containerDims = dims;
	
	
	this.elContent = el;
	this.elContainer.appendChild( el );
	
	this._position();
}catch(e){
	if( GTDDEBUGMODE ) alert("Lightbox content\n" + e.toString());
}
};
Lightbox.prototype._position = function(){
	// Correctly position container, based on its content's dimensions

	var winDims = GIPROTOTYPE.viewport(this._doc, this._gtdinboxPage.window);
	var cDims = GIPROTOTYPE.getDimensions(this.elContainer, this._doc);
				
	this.el.style.height = winDims[1] + "px";
	this.el.style.width = winDims[0] + "px";
	
	var left = (winDims[0]/2) - (cDims.width / 2);
	var top = (winDims[1]/2) - (cDims.height / 2) - 100;
	if( top < 0 ) top = 0;
	this.elContainer.style.left = left + "px";
	this.elContainer.style.top = top + "px";
	
	if( this._containerDims ){
		if( winDims[1]<this._containerDims.height ){
			//this.elContainer.style.height = winDims[1] + "px";
		}else{
			//this.elContainer.style.height = this._containerDims.height + "px";
		}
	}
	
};